﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace zijian666.AnyExtensions;

public struct ValidationSettings
{
    private HashSet<Type>? _ignoreValidationAttributes;
    private bool? _prefixSource;
    private bool? _useAttributeText;

    /// <summary>
    /// 是否忽略空值
    /// </summary>
    public bool IgnoreNullValue { get; set; }

    /// <summary>
    /// 是否添加错误源前缀
    /// </summary>
    public bool PrefixSource { readonly get => _prefixSource ?? true; set => _prefixSource = value; }

    /// <summary>
    /// 是否使用 <seealso cref="DisplayNameAttribute"/> 或 <seealso cref="DescriptionAttribute"/> 特性中的文本代替对象名称
    /// </summary>
    public bool UseAttributeText { readonly get => _useAttributeText ?? true; set => _useAttributeText = value; }

    /// <summary>
    /// 需要忽略的验证规则
    /// </summary>
    public HashSet<Type> IgnoreValidationAttributes
    {
        get => _ignoreValidationAttributes ??= [];
        set => _ignoreValidationAttributes = value;
    }

    internal readonly bool RequireValidation(object validationAttribute)
    {
        if (_ignoreValidationAttributes is null || _ignoreValidationAttributes.Count == 0)
        {
            return true;
        }
        return _ignoreValidationAttributes.Contains(validationAttribute.GetType());
    }

    /// <summary>
    /// 过滤属性
    /// </summary>
    public Func<PropertyInfo, bool>? Filter { get; set; }
}
